﻿//using System;
//using Blockey.GameObjects3D;
//using BlockeyEssentials;
//using BlockeyEssentials.Drawing;
//using BlockeyEssentials.ScreenSystem;
//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Net;

//namespace Blockey.GameplayScreens
//{
//    public class TagMatchScreen3D : MatchScreen3D
//    {
//        public TagMatchScreen3D(ScreenManager oManager)
//            : base(oManager)
//        {
//            Tagger = null;

//            _TagIndicator = new DrawableModel(All.Graphics.GraphicsDevice, oManager.Game.Content, "Models\\Sphere\\Sphere", false);
//        }

//        #region Methods

//        public override void Update()
//        {
//            base.Update();

//            if (Tagger == null)
//            {
//                foreach (var oPerson in m_oPeople)
//                {
//                    Tagger = oPerson;
//                    break;
//                }
//            }

//            foreach (var oPerson in m_oPeople)
//            {
//                if (oPerson != null && oPerson == Tagger)
//                {
//                    //oPerson.PersonColor = Color.Crimson;
//                    //_TagIndicator.Position = oPerson.Position;
//                }
//                else
//                {
//                    //oPerson.PersonColor = Color.Red;
//                }
//            }
//        }

//        protected override void DrawGameTypeElements(SpriteBatch oSB)
//        {
//            if (Tagger == null)
//            {
//                _TagIndicator.SetBasicEffect(null, null, 0.0f);
//            }
//            else
//            {
//                _TagIndicatorRotation += MathHelper.Pi * 0.01f;
//                _TagIndicator.SetBasicEffect(Tagger.Position + Vector3.UnitZ * 2, null, 2);
//                _TagIndicator.SetColor(Color.Blue);
//                _TagIndicator.SetAlpha(0.25f);
//                _TagIndicator.Draw(All.Graphics.GraphicsDevice, Camera);
//            }
//        }

//        public override void ActionWhenFullyAdded()
//        {
//            if (Networking.IsRunning)
//            {
//                HookEvents();
//            }
//        }

//        protected override void SendClientToHost()
//        {
//            base.SendClientToHost();
//        }

//        protected override void SendHostToClients()
//        {
//            var oWriter = Networking.Writer;
//            byte oId = 0;

//            foreach (var oGamer in Networking.Session.AllGamers)
//            {
//                var oPerson = oGamer.Tag as Person3D;
//                if (oPerson != null)
//                {
//                    if (oPerson == Tagger)
//                        oId = oGamer.Id;
//                }
//            }

//            oWriter.Write(oId);

//            base.SendHostToClients();
//        }

//        protected override void ClientReceiveFromHost()
//        {
//            foreach (var oLocal in Networking.Session.LocalGamers)
//            {
//                if (oLocal.IsHost == false)
//                {
//                    while (oLocal.IsDataAvailable)
//                    {
//                        NetworkGamer oHost;
//                        oLocal.ReceiveData(Networking.Reader, out oHost);

//                        byte oID = Networking.Reader.ReadByte();
//                        foreach (var oGamer in Networking.Session.AllGamers)
//                        {
//                            if (oGamer.Id == oID)
//                            {
//                                var oPerson = oGamer.Tag as Person3D;
//                                if (oPerson != null)
//                                    Tagger = oPerson;

//                                break;
//                            }
//                        }

//                        foreach (var oGamer in Networking.Session.AllGamers)
//                        {
//                            var oPerson = oGamer.Tag as Person3D;
//                            var x = (float)Networking.Reader.ReadInt16() * 0.01f;
//                            var y = (float)Networking.Reader.ReadInt16() * 0.01f;
//                            oPerson.PositionDestination = new Vector3(x, y, 0);
//                            oPerson.PrevPosition = oPerson.Position;

//                            var oRotation = oPerson.Rotation;
//                            oRotation.Z = MathHelper.ToRadians(Networking.Reader.ReadInt16());
//                            oPerson.Rotation = oRotation;

//                            var oCondition = (ConditionName)Networking.Reader.ReadByte();
//                            if (oPerson.Condition.Name != oCondition)
//                            {
//                                oPerson.Condition.ChangeTypeForClient(oCondition);
//                                oPerson.StartCondition(false);
//                            }
//                        }
//                    }
//                }
//            }
//        }

//        protected override void HandleCollision(Moveable3D oMoveA, Moveable3D oMoveB)
//        {
//            base.HandleCollision(oMoveA, oMoveB);

//            if (Networking.IsRunning && Networking.Session.IsHost && Tagger != null)
//            {
//                if (Tagger.Condition.Name == ConditionName.Falling)
//                    return;

//                var oSkaterA = oMoveA as Person3D;
//                var oSkaterB = oMoveB as Person3D;

//                if (oSkaterA != null && oSkaterB != null)
//                {
//                    if (Tagger == oSkaterA)
//                        Tagger = oSkaterB;
//                    else if (Tagger == oSkaterB)
//                        Tagger = oSkaterA;
//                }
//            }
//        }

//        #endregion

//        #region Network Session Events

//        private static void HookEvents()
//        {
//            if (Networking.Session.IsHost)
//            {
//                Networking.Session.AllowJoinInProgress = true;
//                Networking.Session.AllowHostMigration = true;
//            }
//            Networking.Session.GamerJoined += new EventHandler<GamerJoinedEventArgs>(session_GamerJoined);
//            Networking.Session.GamerLeft += new EventHandler<GamerLeftEventArgs>(session_GamerLeft);
//            Networking.Session.GameStarted += new EventHandler<GameStartedEventArgs>(session_GameStarted);
//            Networking.Session.HostChanged += new EventHandler<HostChangedEventArgs>(session_HostChanged);
//            Networking.Session.GameEnded += new EventHandler<GameEndedEventArgs>(session_GameEnded);
//        }

//        // Fired when the game ends
//        static void session_GameEnded(object sender, GameEndedEventArgs e)
//        {

//        }

//        // Triggered when the host is changed
//        static void session_HostChanged(object sender, HostChangedEventArgs e)
//        {
//            var oPersonOld = e.OldHost.Tag as Person3D;
//            var oPersonNew = e.NewHost.Tag as Person3D;

//            if (oPersonOld != null && oPersonNew != null)
//            {
//                if (oPersonOld == Tagger)
//                    Tagger = oPersonNew;

//                RemoveObject(oPersonOld);
//                e.OldHost.Tag = null;
//            }
//        }

//        // Triggered when the game starts 
//        static void session_GameStarted(object sender, GameStartedEventArgs e)
//        {

//        }

//        // Triggered when a gamer leaves the game
//        static void session_GamerLeft(object sender, GamerLeftEventArgs e)
//        {
//            var oPerson = e.Gamer.Tag as Person3D;

//            if (Networking.Session.IsHost)
//            {
//                if (oPerson != null && oPerson == Tagger)
//                {
//                    foreach (var oGamer in Networking.Session.AllGamers)
//                    {
//                        var oNext = oGamer.Tag as Person3D;
//                        if (oNext != null)
//                        {
//                            Tagger = oNext;
//                            break;
//                        }
//                    }
//                }
//            }

//            if (oPerson != null)
//            {
//                RemoveObject(oPerson);
//                e.Gamer.Tag = null;
//            }
//        }

//        // Triggered when a gamer joins the game
//        static void session_GamerJoined(object sender, GamerJoinedEventArgs e)
//        {
//            var oGamer = e.Gamer;

//            ControllingPlayer oPlayer;
//            if (e.Gamer.IsLocal)
//                oPlayer = ControllingPlayer.PlayerOne;
//            else
//                oPlayer = new ControllingPlayer(e.Gamer);

//            var oPerson = new Skater(All.CurrentScreenManager.Game, ControllingPlayer.AI, Alliance.Home);

//            AddObject(oPerson);
//            e.Gamer.Tag = oPerson;

//            if (Tagger == null)
//            {
//                if (oPerson != null)
//                    Tagger = oPerson;
//            }
//        }

//        #endregion

//        private static Person3D Tagger
//        {
//            get { return _PersonTag; }
//            set
//            {
//                if (_PersonTag != null)
//                    _PersonTag.Drawable.SetColor(Color.White);

//                _PersonTag = value;

//                if (_PersonTag != null)
//                    //_PersonTag.Drawable.SetColor(Color.Yellow);
//                    _PersonTag.Drawable.SetColor(Color.White);
//            }
//        }

//        private static Person3D _PersonTag;

//        private DrawableModel _TagIndicator;
//        private float _TagIndicatorRotation;

//    }
//}
